/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

var menuElementShow = function(elem, show){
  var object = $(elem).children('.buttonDescription');
  if(object.length > 0){
    if(show){
      object.show(200);
    }
    else{
      object.hide(200);
    }
  }
}

var showSearch = function(elem){
	var elem = $(elem).children('.searchBody');
	if(!elem.is(':visible')){

			elem.show(200);
                        $('.searchTitle').hide();
                        elem.siblings('.searchBodyIcons').show();
		
	}
	else{
            elem.hide(200);
            $('.searchTitle').show();
             elem.siblings('.searchBodyIcons').hide();
		
	}
}

var transportToFilterContainer = function(){
	var object = $('#sf_admin_bar');
	if(object.length > 0){
		$('.searchButton .searchBody').html(object.html());
	}
	else{
		$('.searchButton').hide();
	}	
}

var getPageTitle = function(){
  var object = $('#sf_admin_container h1');
  if(object.length > 0){
    $('.mainContainerTitle h1').html(object.html());
  }
}

var prepareTables = function(){
  var object = $('#sf_admin_content form .sf_admin_list table');
  if(object.length > 0){
    object.addClass('table table-striped table-bordered');
  }  
}

var prepareForms = function(){
  var object = $('.sf_admin_form form');
  if(object.length > 0){
    object.attr('role', 'form');
    $('#sf_fieldset_none .sf_admin_form_row').each(function(){
      var child = $(this).children('div');
      if(child !== undefined){
        child.addClass('form-group');
        child.children('.content').children('input').addClass('form-control');
      }
    });
    $('.sf_admin_form_row .form-group').each(function(){
      var child = $(this).children('label');
      if(child !== undefined){
        child.addClass('specialLabel');
      }
    });
  }
  var button = $('.sf_admin_action_save').children('input');
  if(button.length > 0){
    button.addClass('btn btn-primary biggerBtn');
  }
  $('input[type="submit"]').addClass('btn btn-primary');
  $('input[type="submit"]').attr('value', 'Potwierdź');
}

var prepareActions = function(){
  var action = new Array();
  action[0] = new Array('sf_admin_action_delete','glyphicon-remove');
  action[1] = new Array('sf_admin_action_list','glyphicon-th-list');
  action[2] = new Array('sf_admin_action_edit','glyphicon-edit');
  action[3] = new Array('sf_admin_action_new','glyphicon-plus');
  action[4] = new Array('sf_admin_action_viewpagesattributes','glyphicon-th');
  action[5] = new Array('sf_admin_action_viewproductattribute','glyphicon-th');
  action[6] = new Array('sf_admin_action_viewprice','glyphicon-euro');
  for(var i = 0; i < action.length; i++){
    var obj = $('.' + action[i][0]);
    if(obj.length > 0){
      var html = obj.html();
      obj.html('<span class="glyphicon ' + action[i][1] + '"></span>' + html);
    }
  }
  
}

var activeCheckbox = function(elem){
    var obj = $(elem).parent('td').parent('tr').children('td');
    if(obj.hasClass('activeCheckbox')){
      obj.removeClass('activeCheckbox');
    }
    else{
      obj.addClass('activeCheckbox');
    }
}

$(document).ready(function(){
  transportToFilterContainer();
  getPageTitle();
  prepareTables();
  prepareForms();
  prepareActions();
  $('.menubtn').mouseover(function(){
      menuElementShow(this, true);
  });
  $('.menubtn').mouseleave(function(){
      menuElementShow(this, false);
  });
  $('.searchButton').click(function(){
      showSearch(this);
  });
  $('#closeSearch').click(function(){
      showSearch(this);
  });
  $('.iconsBox a').mouseover(function(){
      $(this).popover('show');
  });
  $('.iconsBox a').mouseleave(function(){
      $(this).popover('hide');
  });
  $('.sf_admin_batch_checkbox').click(function(){
      activeCheckbox(this);
  });
  $('select').each(function(){
    var elem = $(this);
    var width = '60%';
    if(elem.attr('data-width') !== undefined){
      width = elem.attr('data-width');
    }
    else if(elem.width() !== 0) {
      width = elem.width();
      if(width < 20){
        width = '160';
      }
    }
    elem.select2({
      width: width.toString(),
      dropdownAutoWidth : true
    });
  });
});